package com.huilunyingshi.videoprovider.serviceImp;

import com.alibaba.dubbo.config.annotation.Service;
import com.huilunyingshi.videoapi.service.TFilmService;
import com.huilunyingshi.videoprovider.dao.TFilmMapper;
import com.huilunyingshi.videoprovider.dao.TLableMapper;
import com.huilunyingshi.videoprovider.entity.TFilm;
import com.huilunyingshi.videoprovider.entity.TLable;
import com.hullunyingshi.until.ControllerResult;
import com.hullunyingshi.until.PageHelper;
import com.hullunyingshi.until.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class FilmServiceImp implements TFilmService {

    private final Logger logger = LoggerFactory.getLogger(FilmServiceImp.class);



    @Autowired
    private TFilmMapper TFilmMapper;

    @Autowired
    private TLableMapper tLableMapper;

    /**
     * 查询所有电影
     * @param keyword
     * @param curPage
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getFilmList(String keyword, Integer curPage) {
        if (StringTools.strHasNoData(keyword)) {
            keyword = "";
        }
        if (StringTools.strHasNoData(curPage)) {
            curPage = 1;
        }
        Integer pageSize = 20;
        int totalCount = TFilmMapper.getFilmListCount();
        PageHelper pageHelper = new PageHelper(curPage, pageSize, totalCount);
        Integer totalpage = pageHelper.getTotalPage();
        Integer start = pageHelper.getStartIndex(curPage, pageSize);
        ArrayList<TFilm> TfList = TFilmMapper.getFilmList(keyword, start, pageSize);
        pageHelper.setData(TfList);

        return new ControllerResult("200", "查询film列表成功", pageHelper);
    }

    /**
     * 查询电影详情
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getFilm(Long id) {
        try {
            if (StringTools.strHasNoData(id)){
                return new ControllerResult("403", "电影id不能为空", null);
            }

            TFilm tFilm= TFilmMapper.selectByPrimaryKey(id);
            return new ControllerResult("200","点击动漫显示页面成功",tFilm);


        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }

    }

    /**
     * 根据类型查询电影列表
     * @param lid
     * @param curPage
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getFilmTypeList(Long lid,  Integer curPage) {
        try {
            if (StringTools.strHasNoData(lid)){
                return new ControllerResult("403", "类型id不能为空", null);
            }
            if (StringTools.strHasNoData(curPage)) {
                curPage = 1;
            }
            Integer pageSize =20;
            int totalCount = TFilmMapper.getFilmListCount();
            PageHelper pageHelper = new PageHelper(curPage, pageSize, totalCount);
            Integer startIndex = PageHelper.getStartIndex(curPage, pageSize);
            List<Map<String, Object>> filmTypeList = TFilmMapper.getFilmTypeList(lid, startIndex, pageSize);
            pageHelper.setData(filmTypeList);


            return new ControllerResult("200", "查询typefilm列表成功", pageHelper);
        }catch(Exception e) {e.printStackTrace();
            throw new RuntimeException(e.toString());
        }

    }

    /**
     * 多条件筛选查询
     * @param typeName  电影类型名称
     * @param filmReleaseTime  电影上映年代
     * @param filmArea  电影地区
     * @param filmStatus 电影状态（预告，免费）
     * @param curPage
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult getFilmTypeLists(String typeName, String filmReleaseTime, String filmArea, String filmStatus, Integer curPage) {
        try {

            if (StringTools.strHasNoData(curPage)) {
                curPage = 1;
            }
            Long lid = null;
            Long frtId = null;
            Long areaId = null;
            Long fsId = null;
            if (!StringTools.strHasNoData(typeName)) {
                TLable tLable = tLableMapper.selectByTypeName(typeName,1);
                lid = tLable.getId();
            }
            if (!StringTools.strHasNoData(filmReleaseTime)) {
                TLable tLable = tLableMapper.selectByTypeName(filmReleaseTime,1);
                frtId = tLable.getId();
            }
            if (!StringTools.strHasNoData(filmArea)) {
                TLable tLable = tLableMapper.selectByTypeName(filmArea,1);
                areaId = tLable.getId();
            }
            if (!StringTools.strHasNoData(filmStatus)) {
                TLable tLable = tLableMapper.selectByTypeName(filmStatus,1);
                fsId = tLable.getId();
            }
            Integer pageSize = 20;
            int totalCount = TFilmMapper.getFilmListCount();
            PageHelper pageHelper = new PageHelper(curPage, pageSize, totalCount);
            Integer startIndex = PageHelper.getStartIndex(curPage, pageSize);
            List<Map<String, Object>> filmTypeLists = TFilmMapper.getFilmTypeLists(lid, frtId, areaId,fsId ,startIndex, pageSize);
            pageHelper.setData(filmTypeLists);


            return new ControllerResult("200", "查询typefilms列表成功", pageHelper);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }
    }
}
